package com.wufun.overseas.utils;

import android.annotation.SuppressLint;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;

import com.wufun.overseas.R;
import com.wufun.overseas.joystick.map.BlackListManager;
import com.wufun.overseas.main.activity.HandShankNoActivity;
import com.wufun.overseas.main.activity.HandShankOverActivity;
import com.wufun.overseas.main.activity.HandShankYesActivity;
import com.wufun.overseas.main.activity.SetWebViewActivity;
import com.wufun.overseas.main.base.MyApplication;
import com.wufun.overseas.models.bean.IntentDateBean;
import com.wufun.overseas.models.db.HandShankTable;
import com.wufun.overseas.models.greendao.gen.HandShankTableDao;

import java.util.List;

/**
 * Created by yangg on 2017/12/5.
 */

public class IntentUtil {
    private static IntentUtil ourInstance;

    private IntentUtil() {

    }

    public static IntentUtil getInstance() {
        if (null == ourInstance) {
            ourInstance = new IntentUtil();
        }

        return ourInstance;
    }

    public void goWirelessSettings(Context context) {
        Intent intent = null;
        //判断手机系统的版本  即API大于10 就是3.0或以上版本
        intent = new Intent(Settings.ACTION_SETTINGS);
        context.startActivity(intent);
    }

    public void goShareWebActivity(Context context, IntentDateBean bean) {
        String url = bean.getLink_type_val();
        if (!TextUtils.isEmpty(url) && (url.contains(".taobao.com/") || url.contains(".tmall.com/")) && checkInstall(context, "com.taobao.taobao")) {
            Intent intentt = new Intent();
            intentt.setAction("android.intent.action.VIEW");
            Uri uri = Uri.parse(url);
            intentt.setData(uri);
            intentt.setClassName("com.taobao.taobao", "com.taobao.tao.detail.activity.DetailActivity");
            context.startActivity(intentt);
        } else {
//            ShareWebActivity.intent(context).intentdate(intentDateBean).start();
            Intent intent = new Intent(context, SetWebViewActivity.class);
//            intent.putExtra("intentDateBean", bean);
            intent.putExtra("url",url);
            context.startActivity(intent);
        }
    }

    public void goJoystickManager(Context ctx) {
        if (Build.VERSION.SDK_INT < 15) {
            ToastUtils.getInstance(ctx).showToastSystem(ctx.getResources().getString(R.string.mobile_not_support));
            return;
        }
        if (BluetoothAdapter.getDefaultAdapter() == null) {
//            HandShankOverActivity.intent(ctx).flags(Intent.FLAG_ACTIVITY_CLEAR_TOP).start();
            Intent intent = new Intent(ctx, HandShankOverActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            ctx.startActivity(intent);
        } else {
            List<HandShankTable> list = findForTime();
            if (list == null || list.size() == 0) {
//                HandShankNoActivity_.intent(ctx).flags(Intent.FLAG_ACTIVITY_CLEAR_TOP).start();
                Intent intent = new Intent(ctx, HandShankNoActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                ctx.startActivity(intent);
            } else {
//                HandShankYesActivity_.intent(ctx).flags(Intent.FLAG_ACTIVITY_CLEAR_TOP).start();
                Intent intent = new Intent(ctx, HandShankYesActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                ctx.startActivity(intent);
            }
        }
    }

    public void intentActivity(Context context, IntentDateBean bean) {

    }


    //region 辅助方法
    private boolean checkInstall(Context context, String pak) {
        boolean        install = false;
        PackageManager pm      = context.getPackageManager();
        try {
            @SuppressLint("WrongConstant") PackageInfo info = pm.getPackageInfo(pak, 1);
            if (info != null && info.activities.length > 0) {
                install = true;
            }
        } catch (PackageManager.NameNotFoundException e) {
//            e.printStackTrace();
        }
        return install;
    }


    public List<HandShankTable> findForTime() {
        List<HandShankTable> list = null;
        try {
            HandShankTableDao dao = MyApplication.getMyApplication().getSession().getHandShankTableDao();
            list = dao.queryBuilder().orderDesc(HandShankTableDao.Properties.Time).list();
            for (int i = 0; i < list.size(); i++) {
                HandShankTable handShankTable = list.get(i);
                if (BlackListManager.getInstance().isInBlackList(handShankTable.getName())) {
                    dao.delete(handShankTable);
                    list.remove(i);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
    //endregion
}
